package com.ch999.detect.View.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ch999.detect.AppBaseActivity;
import com.ch999.detect.R;
import com.ch999.detect.View.adapter.JiXinNameAdatper;
import com.ch999.detect.View.adapter.PhoneNameAdatper;
import com.ch999.detect.constant.SpType;
import com.ch999.detect.mode.bean.PhoneGoodsMode;
import com.ch999.detect.utils.SharePrefUtil;
import com.google.gson.Gson;
import com.scorpio.mylib.ottoBusProvider.BusEvent;
import com.scorpio.mylib.ottoBusProvider.MyBusProvide;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ch999.detect.constant.EventType.MSG_SELECT_JIXIN_SUCCESS;


public class JiXingSelectActivity extends AppBaseActivity {


    RecyclerView leftRecycler;
    RecyclerView rightRecycle;
    LinearLayout activityJiXingSelect;
    AutoCompleteTextView mAutotext;


    private List<PhoneGoodsMode.DataBean> mPhoneData;

    private List<PhoneGoodsMode.DataBean.ItemsBean> mJixingitems;
    private JiXinNameAdatper mJiXinNameAdatper;
    private int mBrandId;
    private int mPid;


    private ArrayAdapter<String> arrayAdapter;

    private List<String> srarchParmes = new ArrayList<>();

    private Map<String, PhoneGoodsMode.DataBean.ItemsBean> mGoodsPrarms;

    @Override
    public int getLayoutId() {

        return R.layout.activity_ji_xing_select;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        initData();

          mAutotext.setCursorVisible(false);

        mAutotext.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                mAutotext.setCursorVisible(true);
                return false;
            }
        });

        leftRecycler.addOnScrollListener(onScrollListener());
        rightRecycle.addOnScrollListener(onScrollListener());
    }

    @NonNull
    private RecyclerView.OnScrollListener onScrollListener() {
        return new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                mAutotext.setCursorVisible(false);

                super.onScrollStateChanged(recyclerView, newState);
            }
        };
    }


    private void initData() {

        mGoodsPrarms = new HashMap<>();
        String response = SharePrefUtil.getInstance(mContext).getString(SpType.KEY_PHONE_GOOGS);

        if (response == null) {
            return;
        }

        final Gson gson = new Gson();
        PhoneGoodsMode phoneGoodsMode = gson.fromJson(response, PhoneGoodsMode.class);

        if (phoneGoodsMode != null && phoneGoodsMode.getData().size() > 0) {
            mPhoneData = phoneGoodsMode.getData();
            //默认选中第一个
            mPhoneData.get(0).setSeleced(true);
            for (PhoneGoodsMode.DataBean good : mPhoneData) {

                List<PhoneGoodsMode.DataBean.ItemsBean> itemBeans = good.getItems();
                if (itemBeans != null) {

                    for (PhoneGoodsMode.DataBean.ItemsBean itemBean : itemBeans) {

                        String pname = itemBean.getPname();
                        srarchParmes.add(pname);

                        itemBean.setBrandId(good.getBrandId());
                        mGoodsPrarms.put(pname, itemBean);

                    }

                }

            }

            initAdapter();
        }


        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, srarchParmes);

        mAutotext.setAdapter(arrayAdapter);

        mAutotext.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textView = (TextView) view;

                String string = textView.getText().toString();


                PhoneGoodsMode.DataBean.ItemsBean itemsBean = mGoodsPrarms.get(string);

                if (itemsBean != null) {

                    mBrandId = itemsBean.getBrandId();
                    mPid = itemsBean.getPid();
                    selectJiXinSuccess();

                }


            }
        });

    }

    private void initAdapter() {

        final PhoneNameAdatper phoneNameAdatper = new PhoneNameAdatper(mContext, mPhoneData);
        phoneNameAdatper.setOnClickItemListener(new PhoneNameAdatper.OnClickItemListener() {
            @Override
            public void onClickItemListener(int posation) {
                if (mPhoneData != null) {

                    mBrandId = mPhoneData.get(posation).getBrandId();
                    for (PhoneGoodsMode.DataBean dataBean:mPhoneData){
                        dataBean.setSeleced(false);
                    }
                    mPhoneData.get(posation).setSeleced(true);
                    phoneNameAdatper.notifyDataSetChanged();
                    mJixingitems = mPhoneData.get(posation).getItems();
                    setJixingAdapter(mJixingitems);
                }
            }
        });


        leftRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        leftRecycler.setAdapter(phoneNameAdatper);

        mJixingitems = mPhoneData.get(0).getItems();
        if (mJixingitems == null) {
            return;
        }

        mJiXinNameAdatper = new JiXinNameAdatper(mContext, mJixingitems);
        mJiXinNameAdatper.setOnClickItemListener(new JiXinNameAdatper.OnClickItemListener() {
            @Override
            public void onClickItemListener(PhoneGoodsMode.DataBean.ItemsBean jixinBean, int position) {
                mPid = jixinBean.getPid();
                for (PhoneGoodsMode.DataBean.ItemsBean itemsBean: mJixingitems){
                    itemsBean.setSelected(false);
                }
                mJixingitems.get(position).setSelected(true);
                mJiXinNameAdatper.notifyDataSetChanged();
                selectJiXinSuccess();
            }
        });

        rightRecycle.setLayoutManager(new LinearLayoutManager(mContext));
        rightRecycle.setAdapter(mJiXinNameAdatper);


    }

    public void selectJiXinSuccess() {

        Toast.makeText(mContext, "选择成功", Toast.LENGTH_SHORT).show();

        SharePrefUtil.getInstance(mContext).putString(SpType.KEY_BAND_ID, mBrandId + "");
        SharePrefUtil.getInstance(mContext).putString(SpType.KEY_PHONE_PID, mPid + "");

        sendMessage();

        mBaseHandler.sendEmptyMessageDelayed(0, 500);
    }


    private void sendMessage() {
        try {
            BusEvent obtain = new BusEvent();
            obtain.setAction(MSG_SELECT_JIXIN_SUCCESS.ordinal());
            MyBusProvide.getInstance().post(obtain);

            EventBus.getDefault().post(obtain);
        } catch (RuntimeException ex) {
        }



    }


    private void setJixingAdapter(List<PhoneGoodsMode.DataBean.ItemsBean> jixingitems) {

        if (mJiXinNameAdatper == null || jixingitems == null) {

            return;
        }

        mJiXinNameAdatper.setData(jixingitems);

    }


    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.img_back) {
            finish();


        } else if (i == R.id.tv_title) {
        } else if (i == R.id.btn_search) {
            startAnimActivity(SearchJiXinActivity.class);

        }
    }

    @Override
    public void findView() {
        leftRecycler = (RecyclerView)findViewById(R.id.reft_recycler);
         rightRecycle = (RecyclerView)findViewById(R.id.right_recycle);
         activityJiXingSelect = (LinearLayout)findViewById(R.id.activity_ji_xing_select);
         mAutotext = (AutoCompleteTextView)findViewById(R.id.edit_searc_content);

    }
}
